package com.furongji.education.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furongji.common.enums.CourseEnum;
import com.furongji.common.result.Result;
import com.furongji.education.domain.dto.CourseDto;
import com.furongji.education.domain.po.Course;
import com.furongji.common.domain.query.CourseQuery;
import com.furongji.education.domain.vo.CoursePublishVo;
import com.furongji.education.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-19
 */
@RequiredArgsConstructor
@Tag(name = "课程管理")
@RestController
@RequestMapping("/admin/courses")
public class CourseAdminController {

    private final CourseService courseService;

    @Operation(summary = "分页查询课程信息")
    @PostMapping("/pageQuery/{current}/{size}")
    public Result pageCourseQuery(@PathVariable Integer current,
                                  @PathVariable Integer size,
                                  @RequestBody(required = false) CourseQuery courseQuery) {
        IPage<Course> page = new Page<>(current, size);
        IPage<Course> result = courseService.pageCourseQuery(page, courseQuery);
        Map<String, Object> map = new HashMap<>();
        map.put("list", result.getRecords());
        map.put("total", result.getTotal());
        return Result.success(map);
    }

    @Operation(summary = "新增课程")
    @PostMapping("/addCourse")
    public Result addCourse(@RequestBody CourseDto courseDto) {
        System.err.println(courseDto);
        String courseId = courseService.saveCourse(courseDto);
        Map<String, String> map = new HashMap<>();
        map.put("courseId", courseId);
        return Result.success(map);
    }

    @Operation(summary = "根据课程id查询信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId) {
        Course course = courseService.getById(courseId);
        CourseDto courseDto = new CourseDto();
        BeanUtils.copyProperties(course, courseDto);
        return Result.success(courseDto);
    }

    @Operation(summary = "修改课程信息")
    @PutMapping("/updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        boolean flag = courseService.updateById(course);
        if (flag)
            return Result.success();
        else
            return Result.error();
    }

    @Operation(summary = "查询课程发布信息")
    @GetMapping("/getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo courseInfo = courseService.getPublishCourseInfo(id);
        return Result.success(courseInfo);
    }

    @Operation(summary = "最终发布")
    @PutMapping("/publishCourse/{id}")
    public Result publishCourse(@PathVariable String id) {
        Course course = new Course();
        course.setId(id);
        course.setStatus(CourseEnum.Normal);
        if (courseService.updateById(course)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @Operation(summary = "删除课程信息")
    @DeleteMapping("/deleteCourse/{id}")
    public Result delCourse(@PathVariable String id) {
        courseService.deleteCourseById(id);
        return Result.success();
    }

    // feign TODO 后续改用RabbitMQ
    @Operation(summary = "支付成功添加购买数量")
    @PutMapping("/buyCourseCount/{courseId}")
    public Result buyCourseCount(@PathVariable String courseId) {
        courseService.update(null, new LambdaUpdateWrapper<Course>()
                .eq(Course::getId, courseId)
                .setSql("buy_count = buy_count+1"));
        return Result.success();
    }
}
